Repository navigation
Macros: splice holes, record entries, optional fields - #96
Merged
Merged
Conversation
A splice hole stands for any number of items of a list. It is the extension `res.hole.splice`: an expression where an expression is an item (arguments, array and tuple items), a row of a record, and a case of a switch. A block does not continue into a splice hole, so that one after a case of a switch is the next case. Co-authored-by: Jaap Frolich <jfrolich@gmail.com> Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19 Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A splice hole `${...items}` in a quote is lowered by the list it is in:
the arguments of a call or a constructor and the items of an array or a
tuple take an array of code, the fields of a record an array of entries,
the cases of a switch an array of a pattern and its code. A hole with no
items leaves no comma behind. A splice hole anywhere else is an error at
its position.
A record value is built from entries: Code.entry, Code.entryOf and
Code.spread. Code.record takes them, so its fields can have different
types, be optional (`name: ?value`) and follow a spread.
The type of an optional field is the type of `value.field`, an option.
Code.entryOf, Pat.constructor and Type.record agree with it, and
Type.optional and Type.required change a field with its type.
Code.withAttr puts an attribute a macro computes on code.
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
…ibutes Co-authored-by: Jaap Frolich <jfrolich@gmail.com> Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19 Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com> Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19 Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A splice hole out of place, in a quote and outside one; an error in code that was spliced in, which keeps the place it was written at; a field made optional by hand. Co-authored-by: Jaap Frolich <jfrolich@gmail.com> Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19 Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #90.
What
Four things a macro could not write well, with an example of each on the RFC page.
Splice holes.
${...items}in a quote stands for any number of items of a list. What it takes follows from the list:array<code<'a>>f(${...args})array<Code.entry>{id: 1, ${...fields}}array<(pat, code<'a>)>switch x { ${...cases} }A hole with no items leaves no comma behind. A splice hole anywhere else is an error at its position.
Records built from data.
Code.recordtookarray<(string, code<'a>)>: every field had to have the same type, and there was no optional field or spread. It now takes entries:Code.entry(name, value, ~optional=?),Code.entryOf(field, value)andCode.spread(value).Optional fields. The
typof a fieldage?: intisoption<int>, the type ofvalue.age.Code.entryOfwritesage: ?value,Pat.constructorbinds the field of an inline record as an option, andType.recorddeclaresage?: int.Type.optionalandType.requiredchange a field together with its type. A macro that treats every field the same is right for optional fields without a special case; before, a decoder had to know about?or its record did not type check.Attributes on generated code. A quote writes an attribute in front of an expression (
%code(@warning("-27") ${stub})), andCode.withAttr(value, name, ~payload=?)is for one the macro computes.Changes
${...expr}is the extensionres.hole.splice. It parses where a hole does, and as a row of a record and a case of a switch.macros/quote.rs): a splice hole becomesMacro.__spliceItems,__spliceEntriesor__spliceCasesby the list it is in.Macro: the entries,Type.option,Type.optional,Type.required,Code.withAttr,Code.untyped. Spliced code keeps the place it was written at, so an error in it still shows the line of its quote.record,splice,optionalandattrsare new;decode,default,deriveandtypesuse the new API.Code.recordchanges its argument type. Nothing outside this branch uses it.Verification
cargo test --manifest-path compiler-rust/Cargo.toml --test macros --test macro_errors: 30 and 5 tests pass, including the one that compiles every playground example and four new diagnostics intests/macro_errors.npm run smokeandnpm run buildinplayground/: the 17 examples build and run with the compiler in WebAssembly, and the RFC page renders the four new ones at desktop and phone width with no page errors.packages/@rescript/runtimewith this branch changesMacro.mjsandMacro.cjsonly.${...where a block could continue.Created by this Assistant session